module Main {
  type @getConfig = (string): string
  type @isCertMode = (): boolean
  type @getTimestamp = (): string
  type @sign = (map[string]string, map[string]any, map[string]string, string): string
  type @getMerchantCertSN = (): string
  type @getAlipayRootCertSN = (): string
  type @toUrlEncodedRequestBody = (map[string]any, map[string]string): bytes
  type @readAsJson = ($Response, string): object
  type @getAlipayCertSN = (object): string
  type @extractAlipayPublicKey = (string): string
  type @verify = (object, string): boolean
  type @toRespModel = (object): object
  type @getSdkVersion = (): string

  init(); 

  model AlipayOpenPublicMessageContentCreateResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg'),

    contentId: string(name='content_id'),
    contentUrl: string(name='content_url')
  }

  model AlipayOpenPublicMessageContentModifyResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg'),

    contentId: string(name='content_id'),
    contentUrl: string(name='content_url')
  }

  model AlipayOpenPublicMessageTotalSendResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg'),

    messageId: string(name='message_id')
  }

  model Text {
    title: string(name='title'),
    content: string(name='content')
  }

  model Article {
    title?: string(name='title'),
    desc: string(name='desc'),
    imageUrl?: string(name='image_url'),
    url: string(name='url'),
    actionName?: string(name='action_name')
  }

  model Keyword {
    color: string(name='color'),
    value: string(name='value')
  }

  model Context {
    headColor: string(name='head_color'),
    url: string(name='url'),
    actionName: string(name='action_name'),
    keyword1?: Keyword(name='keyword1'),
    keyword2?: Keyword(name='keyword2'),
    first?: Keyword(name='first'),
    remark?: Keyword(name='remark')
  }

  model Template {
    templateId: string(name='template_id'),
    context: Context(name='context')
  }

  model AlipayOpenPublicMessageSingleSendResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg')
  }

  model AlipayOpenPublicLifeMsgRecallResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg')
  }

  model AlipayOpenPublicTemplateMessageIndustryModifyResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg')
  }

  model AlipayOpenPublicSettingCategoryQueryResponse {
    httpBody: string(name='http_body', description='响应原始字符串'),
    code: string(name='code'),
    msg: string(name='msg'),
    subCode: string(name='sub_code'),
    subMsg: string(name='sub_msg'),

    primaryCategory: string(name='primary_category'),
    secondaryCategory: string(name='secondary_category')
  }

  api createImageTextContent(title: string, cover: string, content: string, contentComment: string, ctype: string, benefit: string, extTags: string, loginIds: string): AlipayOpenPublicMessageContentCreateResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.message.content.create',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
      title = title,
      cover = cover,
      content = content,
      could_comment = contentComment,
      ctype = ctype,
      benefit = benefit,
      ext_tags = extTags,
      login_ids = loginIds
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.message.content.create");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api modifyImageTextContent(contentId: string, title: string, cover: string, content: string, couldComment: string, ctype: string, benefit: string, extTags: string, loginIds: string): AlipayOpenPublicMessageContentModifyResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.message.content.modify',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
      content_id = contentId,
      title = title,
      cover = cover,
      content = content,
      could_comment = couldComment,
      ctype = ctype,
      benefit = benefit,
      ext_tags = extTags,
      login_ids = loginIds
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.message.content.modify");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api sendText(text: string): AlipayOpenPublicMessageTotalSendResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.message.total.send',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var textObj: Text = new Text {
      title = '',
      content = text
    };

    var bizParams: map[string]any = {
      msg_type = 'text',
      text = textObj
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.message.total.send");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api sendImageText(articles: [ Article ]): AlipayOpenPublicMessageTotalSendResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.message.total.send',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
      msg_type = 'image-text',
      articles = articles
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.message.total.send");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api sendSingleMessage(toUserId: string, template: Template): AlipayOpenPublicMessageSingleSendResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.message.single.send',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
      to_user_id = toUserId,
      template = template
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.message.single.send");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api recallMessage(messageId: string): AlipayOpenPublicLifeMsgRecallResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.life.msg.recall',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
      message_id = messageId
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.life.msg.recall");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api setIndustry(primaryIndustryCode: string, primaryIndustryName: string, secondaryIndustryCode: string, secondaryIndustryName: string): AlipayOpenPublicTemplateMessageIndustryModifyResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.template.message.industry.modify',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
      primary_industry_code = primaryIndustryCode,
      primary_industry_name = primaryIndustryName,
      secondary_industry_code = secondaryIndustryCode,
      secondary_industry_name = secondaryIndustryName
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.template.message.industry.modify");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }

  api getIndustry(): AlipayOpenPublicSettingCategoryQueryResponse {
    var systemParams: map[string]string = {
      method = 'alipay.open.public.setting.category.query',

      app_id = @getConfig('appId'),
      timestamp = @getTimestamp(),
      format = 'json',
      version = '1.0',
      alipay_sdk = @getSdkVersion(),
      charset = 'UTF-8',
      sign_type = @getConfig('signType'),
      app_cert_sn = @getMerchantCertSN(),
      alipay_root_cert_sn = @getAlipayRootCertSN()
    };

    var bizParams: map[string]any = {
    };

    var textParams: map[string]string = {
    };

    protocol = @getConfig('protocol');
    method = 'POST';
    pathname = '/gateway.do';

    headers = {
      host = @getConfig('gatewayHost'),
      content-type = 'application/x-www-form-urlencoded;charset=utf-8'
    };

    query = {
      sign = @sign(systemParams, bizParams, textParams, @getConfig('merchantPrivateKey')),
      ... systemParams
    };

    body = @toUrlEncodedRequestBody(bizParams, textParams);
  } returns {
    var respMap: object = @readAsJson(__response, "alipay.open.public.setting.category.query");

    if (@isCertMode()) {
      if (@verify(respMap, @extractAlipayPublicKey(@getAlipayCertSN(respMap)))) {
        return @toRespModel(respMap);
      } 
    } else {
      if (@verify(respMap, @getConfig('alipayPublicKey'))) {
        return @toRespModel(respMap);
      } 
    }
    
    throw {
      message = '验签失败，请检查支付宝公钥设置是否正确。'
    }
    
  } runtime {
    connectTimeout = 15000,
    readTimeout = 15000,
    retry = {
      maxAttempts = 0
    }
  }
}